import { shallowRef, ref, reactive } from "vue";

export default function useApi(async_func: Function) {
  const isLoading = shallowRef(false);
  const result = ref([]);
  const exec = async (param: any) => {
    try {
      isLoading.value = true;
      const _result = await async_func(param);
      result.value = _result;
      return result;
    } finally {
      isLoading.value = false;
    }
  };

  return reactive({
    isLoading,
    exec,
    result,
  });
}
